import BaseController from '../../core/controllers/Base.controller';
import storageApi from '../../core/storage/storage';

@Inject('$log', '$state', 'staffService')
class WorkerInfoDetailController extends BaseController {
  constructor() {
    super();

    this.orgType = storageApi.get('orgType');
    const workerId = $state.params.id;
    this.wid = workerId;
    this.selectedIndex = 0;
    this.currentIndex = 0;
    this.$tableTech = {
      fetchTimes: 0,
      isTableLoading: false,
      paginationTech: {
        ITEMS_PER_PAGE: 10,
        MAX_SIZE: 5,
        currentPage: 1,
        total: 0,
      },
    };
    this.$tableDisclosure = {
      fetchTimes: 0,
      isTableLoading: false,
      paginationDisclosure: {
        ITEMS_PER_PAGE: 10,
        MAX_SIZE: 5,
        currentPage: 1,
        total: 0,
      },
    };
    this.$tableInOut = {
      fetchTimes: 0,
      isTableLoading: false,
      paginationInOut: {
        ITEMS_PER_PAGE: 10,
        MAX_SIZE: 5,
        currentPage: 1,
        total: 0,
      },
    };
    this.init(workerId);
    this.$paginationQuery(workerId);
  }

  $paginationQuery(workerId) {
    this.$getWorkerInfoDetail(workerId);
    this.$getWorkerTech(workerId);
    this.$getWorkerDisclosure(workerId);
    this.$getWorkerInOut(workerId);
  }

  // 获取人员基本详情
  $getWorkerInfoDetail(workerId) {
    this.staffService
      .get({ id: workerId })
      .then((res) => {
        this.workerDetailInfo = res.data;
        if (this.workerDetailInfo.personCollectBean.idCardBean.birthdayStr) {
          this.workerDetailInfo.personCollectBean.idCardBean.age =
          this.$ages(this.workerDetailInfo.personCollectBean.idCardBean.birthdayStr);
        }
      });
  }

  /*eslint-disable */
  $getCertifyType(type) {
    switch (type) {
      case '1':
        return 'A证';
        break;
      case '2':
        return 'B证';
        break;
      case '3':
        return 'C证';
        break;
      case '4':
        return '特种作业证';
        break;
      default:
        return;
    }
  }

  // 获得头像src
  $getAvatarSrc() {
    if (this.workerDetailInfo &&  this.workerDetailInfo.personCollectBean
      && this.workerDetailInfo.personCollectBean.idCardBean
      && this.workerDetailInfo.personCollectBean.idCardBean.file
      && this.workerDetailInfo.personCollectBean.idCardBean.file.filePath) {
      const src = `/${this.workerDetailInfo.personCollectBean.idCardBean.file.filePath}`;
      return src;
    } else {
      return require('../../assets/images/' + 'addBt.jpg');
    }
  }

  // 获取人员教育培训信息
  $getWorkerTech(workerId) {
    this.$tableTech.isTableLoading = true;
    this.staffService
      .queryTraining({
        mapParams: { personId: workerId },
        page: this.$tableTech.paginationTech.currentPage,
        size: this.$tableTech.paginationTech.ITEMS_PER_PAGE,
      })
      .then((res) => {
        this.workerTraningInfo = res.rows;
        this.$tableTech.paginationTech.totalItems = res.total;
        this.$tableTech.isTableLoading = false;
        if (res.total < 1) {
          this.$tableTech.fetchTimes += 1;
        }
      });
  }

  // 获取人员安全交底信息
  $getWorkerDisclosure(workerId) {
    this.$tableDisclosure.isTableLoading = true;
    this.staffService
      .queryDisclosure({
        mapParams: { personId: workerId },
        page: this.$tableDisclosure.paginationDisclosure.currentPage,
        size: this.$tableDisclosure.paginationDisclosure.ITEMS_PER_PAGE,
      })
      .then((res) => {
        this.workerDisclosureInfo = res.rows;
        this.$tableDisclosure.paginationDisclosure.totalItems = res.total;
        this.$tableDisclosure.isTableLoading = false;
        if (res.total < 1) {
          this.$tableDisclosure.fetchTimes += 1;
        }
      });
  }

  // 获取人员进出场记录
  $getWorkerInOut(workerId) {
    this.$tableInOut.isTableLoading = true;
    this.staffService
      .queryInOut({
        mapParams: { orgPersonId: workerId },
        page: this.$tableInOut.paginationInOut.currentPage,
        size: this.$tableInOut.paginationInOut.ITEMS_PER_PAGE,
      })
      .then((res) => {
        this.workerInOutInfo = res.rows;
        this.$tableInOut.paginationInOut.totalItems = res.total;
        this.$tableInOut.isTableLoading = false;
        if (res.total < 1) {
          this.$tableInOut.fetchTimes += 1;
        }
      });
  }

  // 处理年龄(放入共有js)
  $ages(str) {
    const year = str.substr(0,4);
    const currentYear = new Date().getFullYear();
    return (currentYear - year);
  }

  switchTab(item, $index) {
    this.selectedIndex = $index;
    this.name = item.name;
  }

  getDownloadSrc(id) {
    return `/sgms/api/v1/common/downLoad/${id}`;
  }

  init(workerId) {
    this.data = {
      isTableLoading: false, // table数据加载状态，true：加载中，false：加载完
      traningCurrentIndex: 0,
      disclosureCurrentIndex: 0,
    };
    this.tabData = [
      {
        id: '0',
        name: '基本信息',
      },
      {
        id: '1',
        name: '安全教育培训',
      },
      {
        id: '2',
        name: '安全技术交底',
      },
      {
        id: '3',
        name: '进出场记录',
      },
    ];
    this.name = '基本信息';
    this.method = {
      traningControlExpand: (index) => {
        if (index % 2 === 1) {
          return;
        }
        if (this.data.traningCurrentIndex === index + 1) {
          this.data.traningCurrentIndex = -1;
        } else {
          this.data.traningCurrentIndex = index + 1;
        }
      },
      disclosureControlExpand: (index) => {
        if (index % 2 === 1) {
          return;
        }
        if (this.data.disclosureCurrentIndex === index + 1) {
          this.data.disclosureCurrentIndex = -1;
        } else {
          this.data.disclosureCurrentIndex = index + 1;
        }
      },
    };
  }
}

export default WorkerInfoDetailController;
